<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\forest\Member;
use app\common\model\MoneyLog;
use app\common\model\ScoreLog;
use app\common\model\User;

/**
 * 定时任务接口
 * @ApiInternal
 */
class Crontab extends Api
{

    //如果$noNeedLogin为空表示所有接口都需要登录才能请求
    //如果$noNeedRight为空表示所有接口都需要验证权限才能请求
    //如果接口已经设置无需登录,那也就无需鉴权了
    //
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = ['*'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];

    /**
     * 季度收益
     *
     */
    public function quarterly_earnings()
    {
        $month = date('m');
        $day = date('d');
        if (in_array($month, ['3', '6', '9']) && $day == '30') {
            $user = User::all(['forest_level' => ['>', 0], 'validitytime' => ['>', time()]]);
            foreach ($user as &$v) {
                $check = MoneyLog::whereTime('createtime', 'today')->where('source_type', 502)->count();
                if (!$check) {
                    $ids = User::where("find_in_set({$v->id},paren_ids)")->column('id');
                    $score = ScoreLog::whereTime('createtime', 'today')->where('type', 2)->whereIn('user_id', array_push($ids, $v->id))->sum('score');
                    if ($score < $v->money) {
                        continue;
                    } else {
                        $member = Member::get($v->forest_level);
                        $money = bcmul($member->price, 0.1, 2);
                        MoneyLog::income($v->id, $money, 2, 502);
                    }
                }
            }
        }
    }
    /**
     * 过期森林会员处理
     *
     */
    // public function expire_member()
    // {
    //     $user = User::all(['forest_level' => ['>', 0], 'validitytime' => ['<', time()]]);
    //     foreach ($user as &$v) {
    //         $v->save(['forest_level' => 0]);
    //     }
    // }
}
